﻿using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class BlueprintStudy : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Blueprint_BlueprintStudy;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<BlueprintStudyReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			if (req.blueprintId.IsNullOrWhiteSpace())
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：blueprintId参数为空）" });
				return;
			}

			//Mod_Blueprint mod_Blueprint = CsvConfig.blueprintList.FirstOrDefault(m => m.Id == req.blueprintId);
			Mod_Blueprint mod_Blueprint = CsvConfig.GetMod_BlueprintInfoById(req.blueprintId);
			if (mod_Blueprint == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：配置表 mod_Blueprint 信息为空）" });
				return;
			}
			//等级判断 5，技能点消耗 10
			if (session.minecraftSessionInfo.player.Level_Convert < mod_Blueprint.Level_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：等级未达到，不能学习）" });
				return;
			}
			if (session.minecraftSessionInfo.player.SkillPoint < mod_Blueprint.Points_Convert)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：技能点不够，不能学习）" });
				return;
			}
			var blueprintInfo = session.minecraftSessionInfo.blueprint.GetBlueprintInfoList().FirstOrDefault(m => m.blueprintId == req.blueprintId);
			if (blueprintInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：内存中 blueprintInfo 信息为空）" });
				return;
			}
			if (blueprintInfo.isStudied)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：该蓝图Id【{blueprintInfo.blueprintId}】已经学习过了）" });
				return;
			}

			session.minecraftSessionInfo.player.SkillPoint -= mod_Blueprint.Points_Convert;
			blueprintInfo.isStudied = true;
			session.minecraftSessionInfo.blueprint.TransformToBlueprintIdAndStudyStr(blueprintInfo);

			var resp = new BlueprintStudyResp
			{
				blueprintId = req.blueprintId,
				curSkillPoint = session.minecraftSessionInfo.player.SkillPoint
			};
			session.Send(defCommand, req, resp);
		}
	}
}
